'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, BOOLEAN, DATE, DECIMAL, ENUM } = app.Sequelize;

  const Reminder = app.model.define('reminder', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID',
    },
    type: {
      type: ENUM('laundry', 'seasonal', 'shopping', 'outfit', 'maintenance', 'custom'),
      allowNull: false,
      comment: '提醒类型',
    },
    title: {
      type: STRING(200),
      allowNull: false,
      comment: '提醒标题',
    },
    description: {
      type: TEXT,
      comment: '提醒描述',
    },
    clothing_id: {
      type: INTEGER,
      comment: '关联的衣服ID',
    },
    outfit_id: {
      type: INTEGER,
      comment: '关联的搭配ID',
    },
    reminder_date: {
      type: DATE,
      allowNull: false,
      comment: '提醒时间',
    },
    repeat_type: {
      type: ENUM('none', 'daily', 'weekly', 'monthly', 'yearly'),
      defaultValue: 'none',
      comment: '重复类型',
    },
    repeat_interval: {
      type: INTEGER,
      defaultValue: 1,
      comment: '重复间隔',
    },
    is_completed: {
      type: BOOLEAN,
      defaultValue: false,
      comment: '是否已完成',
    },
    completed_at: {
      type: DATE,
      comment: '完成时间',
    },
    is_active: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '是否激活',
    },
    priority: {
      type: ENUM('low', 'medium', 'high'),
      defaultValue: 'medium',
      comment: '优先级',
    },
    metadata: {
      type: TEXT,
      comment: '额外数据(JSON)',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
  }, {
    tableName: 'reminders',
    timestamps: true,
    paranoid: false,  // 禁用软删除
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '提醒表',
  });

  // 关联关系
  Reminder.associate = function() {
    // 属于用户
    app.model.Reminder.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });

    // 关联衣服
    app.model.Reminder.belongsTo(app.model.Clothing, {
      foreignKey: 'clothing_id',
      as: 'clothing',
    });

    // 关联搭配
    app.model.Reminder.belongsTo(app.model.Outfit, {
      foreignKey: 'outfit_id',
      as: 'outfit',
    });
  };

  return Reminder;
};
